﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace Common
{
    public class PrintDoc
    {
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="img"></param>
        /// <param name="Content"></param>
        public Image CreateQuickMark(string Content)
        {
            if (string.IsNullOrEmpty(Content))
            {
                System.Windows.Forms.MessageBox.Show("输入内容不能为空");
                return null;
            }

            EncodingOptions options = null;
            BarcodeWriter writer = null;
            options = new QrCodeEncodingOptions
            {
                DisableECI = true,
                CharacterSet = "UTF-8",
                Width = 100,
                Height = 100
            };
            writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;

            Bitmap bitmap = writer.Write(Content);
            return bitmap;
        }

        private Hashtable _printHash;

        public Hashtable PrintHash
        {
            get
            {
                return _printHash; 
            }
            set
            {
                _printHash = value;                
            }
        }
        public PrintDoc()
        {}
        
        public PrintDoc(Hashtable ht)
        {
            this.PrintHash = ht;
        }
        public void MyPrintDocument(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Image img = (Image)this._printHash["二维码"];
            Rectangle bestFit = new Rectangle(690, 0, 80, 80);
            e.Graphics.Clear(Color.White);
            Font _font = new Font(new FontFamily("宋体"),16);
            e.Graphics.DrawString(this._printHash["凭证"].ToString(), new Font(new FontFamily("宋体"),18), Brushes.Black, 422, 45);
            e.Graphics.DrawString(this._printHash["码头"].ToString(), _font, Brushes.Black, 225, 90);            
            e.Graphics.DrawString(this._printHash["票号"].ToString(), _font, Brushes.Black, 650, 100);
            e.Graphics.DrawImage(img, bestFit);
            e.Graphics.DrawString(this._printHash["社名"].ToString(), _font, Brushes.Black, 185, 134);
            e.Graphics.DrawString(this._printHash["导游"].ToString(), _font, Brushes.Black, 685, 134);
            e.Graphics.DrawString(this._printHash["进园"].ToString(), _font, Brushes.Black, 372, 167);
            e.Graphics.DrawString(this._printHash["成人"].ToString(), _font, Brushes.Black, 372, 207);
            e.Graphics.DrawString(this._printHash["学生"].ToString(), _font, Brushes.Black, 568, 207);
            e.Graphics.DrawString(this._printHash["开票人"].ToString(), _font, Brushes.Black, 685, 240);
            e.Graphics.DrawString("开票时间:" + DateTime.Now.ToLocalTime().ToString(), _font, Brushes.Black, 500, 275);
            //e.Graphics.Dispose();
        }

    }
}
